using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace MONA.Modules.Marketing.BLL
{
    public partial class Comment
    {
        #region Private members.

        protected int _ID;
        protected int _ServiceID;
        protected string _Content = string.Empty;
        protected int _LastCommentID;
        protected DateTime _CreatedDate;
        protected int _CreatedByByUserID;

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public int ID
        {
            set { this._ID = value; }
            get { return this._ID; }
        }

        public int ServiceID
        {
            set { this._ServiceID = value; }
            get { return this._ServiceID; }
        }

        public string Content
        {
            set { this._Content = value; }
            get { return this._Content; }
        }

        public int LastCommentID
        {
            set { this._LastCommentID = value; }
            get { return this._LastCommentID; }
        }

        public DateTime CreatedDate
        {
            set { this._CreatedDate = value; }
            get { return this._CreatedDate; }
        }

        public int CreatedByByUserID
        {
            set { this._CreatedByByUserID = value; }
            get { return this._CreatedByByUserID; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static Comment Load(int iD)
        {
            string spName = "[dbo].p_MARKETING_Comment_Load";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, iD);
            Comment entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new Comment();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ServiceID"))) entity.ServiceID = reader.GetInt32(reader.GetOrdinal("ServiceID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Content"))) entity.Content = reader.GetString(reader.GetOrdinal("Content"));
                if (!reader.IsDBNull(reader.GetOrdinal("LastCommentID"))) entity.LastCommentID = reader.GetInt32(reader.GetOrdinal("LastCommentID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedByByUserID"))) entity.CreatedByByUserID = reader.GetInt32(reader.GetOrdinal("CreatedByByUserID"));
            }
            reader.Close();
            dbCommand.Connection.Close();

            return entity;
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectBy_CreatedByByUserID(int createdByByUserID)
        {
            string spName = "[dbo].p_MARKETING_Comment_SelectBy_CreatedByByUserID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CreatedByByUserID", SqlDbType.Int, createdByByUserID);

            return db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------
        public static DataSet SelectBy_LastCommentID(int lastCommentID)
        {
            string spName = "[dbo].p_MARKETING_Comment_SelectBy_LastCommentID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@LastCommentID", SqlDbType.Int, lastCommentID);

            return db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------

        public static DataSet SelectAll()
        {
            string spName = "[dbo].p_MARKETING_Comment_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll()
        {
            string spName = "[dbo].p_MARKETING_Comment_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_MARKETING_Comment_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_MARKETING_Comment_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int Insert(int serviceID, string content, int lastCommentID, DateTime createdDate, int createdByByUserID)
        {
            Comment entity = new Comment();
            entity.ServiceID = serviceID;
            entity.Content = content;
            entity.LastCommentID = lastCommentID;
            entity.CreatedDate = createdDate;
            entity.CreatedByByUserID = createdByByUserID;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            string spName = "[dbo].p_MARKETING_Comment_Insert";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
            db.AddInParameter(dbCommand, "@ServiceID", SqlDbType.Int, this._ServiceID);
            db.AddInParameter(dbCommand, "@Content", SqlDbType.NText, this._Content);
            db.AddInParameter(dbCommand, "@LastCommentID", SqlDbType.Int, this._LastCommentID);
            db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, this._CreatedDate);
            db.AddInParameter(dbCommand, "@CreatedByByUserID", SqlDbType.Int, this._CreatedByByUserID);

            if (transaction != null)
            {
                db.ExecuteNonQuery(dbCommand, transaction);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
            else
            {
                db.ExecuteNonQuery(dbCommand);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int Update(int iD, int serviceID, string content, int lastCommentID, DateTime createdDate, int createdByByUserID)
        {
            Comment entity = new Comment();
            entity.ID = iD;
            entity.ServiceID = serviceID;
            entity.Content = content;
            entity.LastCommentID = lastCommentID;
            entity.CreatedDate = createdDate;
            entity.CreatedByByUserID = createdByByUserID;
            return entity.Update();
        }

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            string spName = "[dbo].p_MARKETING_Comment_Update";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);
            db.AddInParameter(dbCommand, "@ServiceID", SqlDbType.Int, this._ServiceID);
            db.AddInParameter(dbCommand, "@Content", SqlDbType.NText, this._Content);
            db.AddInParameter(dbCommand, "@LastCommentID", SqlDbType.Int, this._LastCommentID);
            db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, this._CreatedDate);
            db.AddInParameter(dbCommand, "@CreatedByByUserID", SqlDbType.Int, this._CreatedByByUserID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int Delete(int iD)
        {
            Comment entity = new Comment();
            entity.ID = iD;
            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            string spName = "[dbo].p_MARKETING_Comment_Delete";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        #endregion
    }
}